<?php

/**
 * @Desc:账户信息修改接口
 * @author dasheng@baofu.com(大圣)
 * @datetime 2025/4/6 18:45
 */
require_once "../../Init/init.php";

use App\baocaitong3\Entity\AccInfoEntity as AccInfo;
use App\baocaitong3\Entity\ContentBodyEntity as ContentBody;
use App\baocaitong3\Entity\HeaderEntity as Header;
use App\baocaitong3\Entity\PostParmEntity as PostParm;
use App\baocaitong3\Entity\PublicPostEntity as PublicPost;
use App\baocaitong3\SecurityUtil\NewRsaUtil;
use App\baocaitong3\Util\HttpClient;
use App\baocaitong3\Util\Log;
use App\baocaitong3\Util\Tools;

$ServiceTp = "BCT3-1005-001-01";

$contractno = "CM610000000000175288";
$newcardno = "41192571707843";//账号

$accType = 2;//账户类型:1-个人,2-企业/个体

$mobileno ="13561802564";//对私手机号

$BankName="中国农业银行";//银行名称
$DepositBankProvince ="江苏省";//开户行省份
$DepositBankCity = "南京市";//开户行城市
$DepositBankName = "支行分理处";//开户支行名称

$headerinfo = new Header();
$headerinfo->terminalId = $GLOBALS["TerminalId"];
$headerinfo->memberId = $GLOBALS["MemberId"];
$headerinfo->serviceTp = $ServiceTp;
$headerinfo->verifyType = "1";



$accinfo = new AccInfo();
$accinfo->contractNo = $contractno;
$accinfo->cardNo = $newcardno;
$accinfo->transSerialNo = Tools::getTsnOderid("TSN");
if($accType == 1)
{
    $accinfo->mobileNo = $mobileno;
}else if($accType == 2){
    $accinfo->bankName = $BankName;
    $accinfo->depositBankProvince = $DepositBankProvince;
    $accinfo->depositBankCity = $DepositBankCity;
    $accinfo->depositBankName = $DepositBankName;
    // 当开个体户且绑定对私卡时必传corporateMobile,本实例只有对公
    // $accinfo->corporateMobile = $mobileno;
}else throw new Exception("类型不存在");

$modbody = new PublicPost();
$modbody->version = "1.0.0";
$modbody->accType = $accType;
$modbody->optType = "02";//02:提现卡修改
$modbody->accInfo = $accinfo;

$bodycontent = new ContentBody();
$bodycontent->header = $headerinfo;
$bodycontent->body = $modbody;

$ContentStr = json_encode($bodycontent);
Log::EchoFormat("JSON序列号：".$ContentStr);
$enContent = NewRsaUtil::encryptByPfx($ContentStr,$GLOBALS["PFX_PATH"],$GLOBALS["KeyPwd"]);

$postParm = new PostParm();
$postParm->memberId = $GLOBALS["MemberId"];
$postParm->terminalId = $GLOBALS["TerminalId"];
$postParm->content = $enContent;


$ResultPost  = HttpClient::Post($postParm,"https://vgw.baofoo.com/union-gw/api/".$ServiceTp."/transReq.do");
Log::EchoFormat("返回参数：".$ResultPost);
if(empty($ResultPost)) throw new Exception("请求返回异常");

if(strpos($ResultPost,"header") != false)
{
    $resulterror = json_decode($ResultPost,true);
    var_dump($resulterror);
}else{
    $DePostString = NewRsaUtil::decrptByPub($ResultPost,$GLOBALS["CER_PATH"]);
    Log::EchoFormat("解密返回：".$DePostString);
    if(empty($DePostString))  throw new Exception("解密空值，请检查证书是否正常");
    if(strpos($DePostString,"header"))
    {
        $contenBe = json_decode($DePostString,true);
        if($contenBe["header"]["sysRespCode"] === "S_0000")
        {
            Log::EchoFormat("业务层结果:".json_encode($contenBe["header"]));
            if($contenBe["body"]["retCode"] === 1){
                Log::EchoFormat("结果:修改成功".json_encode($contenBe["body"]));
            }else{
                Log::EchoFormat("结果:修改失败".json_encode($contenBe["body"]));
            }
        }
    }
}